home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 13 The Compute Shader / SobelFilter / Shaders / WaveSim.hlsl < prev   
Encoding:
Text File  |  2016-03-02  |  2.1 KB  |  67 lines

  1. //=============================================================================
  2. // WaveSim.hlsl by Frank Luna (C) 2011 All Rights Reserved.
  3. //
  4. // UpdateWavesCS(): Solves 2D wave equation using the compute shader.
  5. //
  6. // DisturbWavesCS(): Runs one thread to disturb a grid height and its
  7. //     neighbors to generate a wave. 
  8. //=============================================================================
  9.  
  10. // For updating the simulation.
  11. cbuffer cbUpdateSettings
  12. {
  13.     float gWaveConstant0;
  14.     float gWaveConstant1;
  15.     float gWaveConstant2;
  16.     
  17.     float gDisturbMag;
  18.     int2 gDisturbIndex;
  19. };
  20.  
  21. RWTexture2D<float> gPrevSolInput : register(u0);
  22. RWTexture2D<float> gCurrSolInput : register(u1);
  23. RWTexture2D<float> gOutput       : register(u2);
  24.  
  25. [numthreads(16, 16, 1)]
  26. void UpdateWavesCS(int3 dispatchThreadID : SV_DispatchThreadID)
  27. {
  28.     // We do not need to do bounds checking because:
  29.     //     *out-of-bounds reads return 0, which works for us--it just means the boundary of 
  30.     //    our water simulation is clamped to 0 in local space.
  31.     //   *out-of-bounds writes are a no-op.
  32.     
  33.     int x = dispatchThreadID.x;
  34.     int y = dispatchThreadID.y;
  35.  
  36.     gOutput[int2(x,y)] = 
  37.         gWaveConstant0 * gPrevSolInput[int2(x,y)].r +
  38.         gWaveConstant1 * gCurrSolInput[int2(x,y)].r +
  39.         gWaveConstant2 *(
  40.             gCurrSolInput[int2(x,y+1)].r + 
  41.             gCurrSolInput[int2(x,y-1)].r + 
  42.             gCurrSolInput[int2(x+1,y)].r + 
  43.             gCurrSolInput[int2(x-1,y)].r);
  44. }
  45.  
  46. [numthreads(1, 1, 1)]
  47. void DisturbWavesCS(int3 groupThreadID : SV_GroupThreadID,
  48.                     int3 dispatchThreadID : SV_DispatchThreadID)
  49. {
  50.     // We do not need to do bounds checking because:
  51.     //     *out-of-bounds reads return 0, which works for us--it just means the boundary of 
  52.     //    our water simulation is clamped to 0 in local space.
  53.     //   *out-of-bounds writes are a no-op.
  54.     
  55.     int x = gDisturbIndex.x;
  56.     int y = gDisturbIndex.y;
  57.  
  58.     float halfMag = 0.5f*gDisturbMag;
  59.  
  60.     // Buffer is RW so operator += is well defined.
  61.     gOutput[int2(x,y)]   += gDisturbMag;
  62.     gOutput[int2(x+1,y)] += halfMag;
  63.     gOutput[int2(x-1,y)] += halfMag;
  64.     gOutput[int2(x,y+1)] += halfMag;
  65.     gOutput[int2(x,y-1)] += halfMag;
  66. }
  67.